package cn.com.Servlet.SuperAdminServlet;

import cn.com.controler.Admin.AdminDAO;
import cn.com.controler.State.StateDAO;
import cn.com.controler.StateView.StateViewDAO;
import cn.com.controler.Util.TimeTr;
import cn.com.controler.User.UserDAO;
import cn.com.entity.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@WebServlet(name = "SuperAdminServlet" , value = "/SuperAdminServlet")
public class SuperAdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        AdminDAO adminDAO = new AdminDAO();
        if(type.equals("add")){       //超级管理员添加普通管理员
            String uname = request.getParameter("uname");
            String pass = request.getParameter("pass");
            String emai = request.getParameter("emai");
            Admin admin = new Admin(1,uname,pass,emai,"2020",0);
            int addadmin = adminDAO.addadmin(admin);
            if(addadmin > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("xg")){        //修改管理员信息
            HttpSession session = request.getSession();
            Admin admin = (Admin)session.getAttribute("admin");
            Integer adminId = admin.getAdminId();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            int updateupe = adminDAO.updateupe(adminId, username, password, email);
            if(updateupe > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("fbgg")){        //发布公告
            String content = request.getParameter("content");
            HttpSession session = request.getSession();
            Admin admin = (Admin)session.getAttribute("admin");
            Integer adminId = admin.getAdminId();
            Date date1 = new Date();
            String date = TimeTr.Date_String(date1);
            State state = new State(1,adminId,content,date);
            StateDAO stateDAO = new StateDAO();
            int i = stateDAO.fbgonggao(state);
            if(i > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        AdminDAO adminDAO = new AdminDAO();
        UserDAO userDAO = new UserDAO();
        if(type.equals("alladmin")){           //查询所有管理员
            List<Admin> selectall = adminDAO.selectall();
            request.setAttribute("admins",selectall);
            request.getRequestDispatcher("/WebContent/HTML/SuperAdminPage/superadmin_admin/glalladmin.jsp").forward(request,response);
        }else if(type.equals("getadmin")){      //查询管理员信息
            HttpSession session = request.getSession();
            Admin admin = (Admin) session.getAttribute("admin");
            Integer id = Integer.valueOf(admin.getAdminId());
            List<Admin> selectbyid = adminDAO.selectbyid(id);
            Admin admin1 = selectbyid.get(0);
            request.setAttribute("admin",admin1);
            request.getRequestDispatcher("/WebContent/HTML/SuperAdminPage/superadmin_admin/alterdata.jsp").forward(request,response);
        }else if(type.equals("delete")){        //取消普通管理员权限
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            int deletebyid = adminDAO.deletebyid(id);
            if(deletebyid >0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("selectalladmin")){        //查询所有普管
            List<Admin> selectalladmin = adminDAO.selectalladmin();
            request.setAttribute("alladmins",selectalladmin);
            request.getRequestDispatcher("/WebContent/HTML/SuperAdminPage/superadmin_admin/addsuperadmin.jsp").forward(request,response);
        }else if(type.equals("shengji")){        //升级超管
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            int shengjibyid = adminDAO.shengjibyid(id);
            if(shengjibyid >0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("allusers")){        //查询所有用户
            List<User> selectalluser = userDAO.selectalluser();
            request.setAttribute("users",selectalluser);
            request.getRequestDispatcher("/WebContent/HTML/SuperAdminPage/superadmin_user/glalluser.jsp").forward(request,response);
        }else if(type.equals("delbyid")){        //id删除用户
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            int deletebyid = userDAO.deletebyid(id);
            if(deletebyid > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("selectbyuname")){        //用户名查询
            String username = request.getParameter("username");
            List upselectbyuname = userDAO.upselectbyuname(username);
            JSONArray jsonArray = JSONArray.fromObject(upselectbyuname);
            response.getWriter().write(jsonArray.toString());
        }else if(type.equals("ggcx")){          //查询公告
            StateViewDAO stateViewDAO = new StateViewDAO();
            StateView stateView = stateViewDAO.selectallState();
            JSONObject jsonObject = JSONObject.fromObject(stateView);
            PrintWriter writer = response.getWriter();
            writer.write(jsonObject.toString());
            writer.flush();
            writer.close();
        }else if(type.equals("ggsc")){         //根据id删除公告
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            StateViewDAO stateViewDAO = new StateViewDAO();
            int delbyid = stateViewDAO.delbyid(id);
            if(delbyid > 0 ){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }

        }
    }
}
